package com.example.mq.server.core;

import com.example.mq.common.ConsumerEnv;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.atomic.AtomicInteger;

/*
* 消息队列
* */
public class MSGQueue{
    // 表示队列的唯一标识
    private String name;
    // 表示队列是否持久化
    private boolean durable = false;
    // 表示独有的,独占的 为true表示只能有一个消费者使用
    private boolean exclusive = false;
    // 为true表示没人使用后,自动删除
    private boolean autoDelete = false;
    private Map<String,Object> arguments = new HashMap<>();
    // 当前队列都有哪些消费者订阅了
    private final List<ConsumerEnv> consumerEnvList = new ArrayList<>();
    // 针对索引进行原子操作(加锁)
    private AtomicInteger consumerIndex = new AtomicInteger(0);

    public void addConsumer(ConsumerEnv consumerEnv){
        // 针对当前队列进行加锁
        consumerEnvList.add(consumerEnv);
    }

    // 挑选消费者,使用轮询
    public ConsumerEnv chooseConsumer(){
        if(consumerEnvList.size() == 0){
            return null;
        }
        int index = consumerIndex.get()%consumerEnvList.size();
        consumerIndex.getAndIncrement(); // 自增
        return consumerEnvList.get(index);
    }

    public MSGQueue() {
    }

    public MSGQueue(String name, boolean durable, boolean exclusive, boolean autoDelete) {
        this.name = name;
        this.durable = durable;
        this.exclusive = exclusive;
        this.autoDelete = autoDelete;
    }

    public String getArguments() {
        ObjectMapper objectMapper = new ObjectMapper();
        try {
            return objectMapper.writeValueAsString(arguments);
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
        // 如果真的发生了异常,返回空的json字符串
        return "{}";
    }

    // 这个方法是从数据库读取后,构造Exchange对象时自动调用
    public void setArguments(String argumentsJson) {
        // 把参数中的argumentsJson转成Map
        ObjectMapper objectMapper = new ObjectMapper();
        try {
            this.arguments = objectMapper.readValue(argumentsJson, new TypeReference<HashMap<String,Object>>() {});
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
    }

    // 针对arguments在提供一对get,set方法,为了更加方便的获取和设置键值对
    public void setArguments(String key,Object value) {
        arguments.put(key,value);
    }

    public void setArguments(Map<String,Object> arguments){
        this.arguments = arguments;
    }

    public Object getArguments(String key){
        return arguments.get(key);
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public boolean isDurable() {
        return durable;
    }

    public void setDurable(boolean durable) {
        this.durable = durable;
    }

    public boolean isExclusive() {
        return exclusive;
    }

    public void setExclusive(boolean exclusive) {
        this.exclusive = exclusive;
    }

    public boolean isAutoDelete() {
        return autoDelete;
    }

    public void setAutoDelete(boolean autoDelete) {
        this.autoDelete = autoDelete;
    }

}
